﻿using System;
using System.Collections.Generic;
using System.IO;

// 日志接口
public interface ILogger
{
    void Log(string message);
}

// 控制台日志记录器
public class ConsoleLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine($"ConsoleLogger: {message}");
    }
}

// 文件日志记录器
public class FileLogger : ILogger
{
    private readonly string _filePath = "log.txt";

    public void Log(string message)
    {
        File.AppendAllText(_filePath, $"FileLogger: {message}\n");
    }
}

// 数据库日志记录器（模拟）
public class DatabaseLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine($"DatabaseLogger: {message} (simulated)");
    }
}

// 日志工厂
public abstract class LoggerFactory
{
    public abstract ILogger CreateLogger();
}

// 控制台日志工厂
public class ConsoleLoggerFactory : LoggerFactory
{
    public override ILogger CreateLogger()
    {
        return new ConsoleLogger();
    }
}

// 文件日志工厂
public class FileLoggerFactory : LoggerFactory
{
    public override ILogger CreateLogger()
    {
        return new FileLogger();
    }
}

// 数据库日志工厂
public class DatabaseLoggerFactory : LoggerFactory
{
    public override ILogger CreateLogger()
    {
        return new DatabaseLogger();
    }
}

// 装饰器基类
public abstract class LoggerDecorator : ILogger
{
    protected readonly ILogger _logger;

    protected LoggerDecorator(ILogger logger)
    {
        _logger = logger;
    }

    public abstract void Log(string message);
}

// 时间戳装饰器
public class TimestampLogger : LoggerDecorator
{
    public TimestampLogger(ILogger logger) : base(logger) { }

    public override void Log(string message)
    {
        _logger.Log($"{DateTime.Now}: {message}");
    }
}

// 日志级别装饰器
public class LevelLogger : LoggerDecorator
{
    private readonly string _level;

    public LevelLogger(ILogger logger, string level) : base(logger)
    {
        _level = level;
    }

    public override void Log(string message)
    {
        _logger.Log($"[{_level}] {message}");
    }
}

// 日志管理器 - 使用观察者模式
public class LogManager
{
    private readonly List<ILogger> _loggers = new();

    // 添加日志记录器
    public void AddLogger(ILogger logger)
    {
        _loggers.Add(logger);
    }

    // 记录日志
    public void Log(string message)
    {
        foreach (var logger in _loggers)
        {
            logger.Log(message);
        }
    }
}

// 测试
class Program
{
    static void Main()
    {
        // 创建日志管理器
        LogManager logManager = new();

        // 创建不同的日志记录器并添加到日志管理器
        LoggerFactory consoleFactory = new ConsoleLoggerFactory();
        LoggerFactory fileFactory = new FileLoggerFactory();
        LoggerFactory dbFactory = new DatabaseLoggerFactory();

        ILogger consoleLogger = new TimestampLogger(new LevelLogger(consoleFactory.CreateLogger(), "INFO"));
        ILogger fileLogger = new TimestampLogger(new LevelLogger(fileFactory.CreateLogger(), "ERROR"));
        ILogger dbLogger = new TimestampLogger(new LevelLogger(dbFactory.CreateLogger(), "DEBUG"));

        logManager.AddLogger(consoleLogger);
        logManager.AddLogger(fileLogger);
        logManager.AddLogger(dbLogger);

        // 记录日志
        logManager.Log("This is a test log message.");
    }
}

